;/**/

var factory = function(){
	/*单选题*/
	this.radio=$('#radiomodel');
	/* 多选*/
	this.checkbox=$('#checkboxmodel');
	/*判断*/
	this.judge=$('#judgemodel');
	/*填空*/
	this.completion=$('#completionmodel');
	/*简答*/
	this.shotanswer=$('#shotanswer');
	/*  元素编号 */
	this.radioid=0;
	this.checkboxid=0;
	this.judgeid=0;
	this.completionid=0;
	this.bossid = 0;
	var that = this;
	this.createRadio=function(){/*创建移到单选 */
		var radio = $('<div>');
		radio.append(this.radio.html());
		var ans = radio.find('input[name=radio_ans]');
		var qs = radio.find('input[name=radio_q]');
		radio.find('input[name=radio_title]').attr('name','radio_title['+this.radioid+']');
		for (var i = ans.length - 1; i >= 0; i--) {
			ans.eq(i).attr('name','radio_ans['+this.radioid+']');
			qs.eq(i).attr('name','radio_q['+this.radioid+"][]");
		}
		this.radioid++;
		return radio;
	};
	this.createCheckbox=function(){/*创建多选*/
		var radio = $('<div>');
		radio.append(this.checkbox.html());
		var q = radio.find('input[name=box_q]');
		var ans = radio.find('input[name=box_ans]').attr('name','box_ans['+this.checkboxid+']');
		radio.find('input[name=box_title]').attr('name','box_title['+this.checkboxid+']');
		for (var i = q.length - 1; i >= 0; i--) {
			q.eq(i).attr('name','box_q['+this.checkboxid+"][]");
			
		}
		this.checkboxid++;
		return radio;
	};
	this.createJudge=function(){/*创建判断*/
		var judge = $('<div>');
		judge.append(this.judge.html());
		judge.find('input[name=judge_title]').attr('name','judge_title['+this.judgeid+']');
		var ans = judge.find('input[name=judge_ans]');
		for (var i = ans.length - 1; i >= 0; i--) {
			ans.eq(i).attr('name','judge_ans['+this.judgeid+']');
			
		}
		
		this.judgeid++;
		return judge;
	};
	this.createCompletion = function(){/*创填空题*/
		var comp =  $('<div>');
		comp.append(this.completion.html());
		this.completionid++;
		return comp;
	}
	this.createBoss = function(){/*创建大题*/
		var comp =  $('<div>');
		comp.append(this.shotanswer.html());
		this.bossid++;
		return comp;
	}
	this.empty = function() {
		if (
			this.radioid == 0 ||
			this.checkboxid == 0 ||
			this.judgeid == 0 ||
			this.completionid == 0 ||
			this.bossid == 0
		) {
			return true;
		} else {
			return false;
		}
	}

	
}
var calccon = function(){
	var that = this;
	// 显示数量的位置
	this.selnum = $('#selnum');
	this.chenum = $('#chenum');
	this.cmpnum = $('#cmpnum');
	this.judnum = $('#judnum');
	this.bosnum = $('#bosnum');
	this.fuma = $('#fuma');
	/* 分值*/
	this.selscore = 0;
	this.chescore = 0;
	this.judscore = 0;
	this.cmpscore = 0;
	this.bosscore = 0;
	/*总分*/
	this.scoretotal = $('#scoretotal');
	this.inps = $('#calcAll').find('input');
	this.calc = function(){
		this.fuzhi();
		var a= factory.radioid;/* 获取工厂生成的数量 */
		var b= factory.checkboxid;
		var c= factory.judgeid;
		var d= factory.completionid;
		var e= factory.bossid;
		var total = a*this.selscore + b * this.chescore + c * this.cmpscore + d * this.judscore + e * this.bosscore;

/* 显示位置 赋值 */
		this.selnum.html(a)
		this.chenum.html(b);
		this.judnum.html(c);
		this.cmpnum.html(d);
		this.bosnum.html(e);
	// 记总分
		this.scoretotal.html(total);
		this.fuma.val(total);
		return '';
	}
	this.fuzhi = function(){/* 重新或获取值*/
		/* 分值*/
		this.selscore = $("input[sign=a]").val();
		this.chescore = $("input[sign=b]").val();
		this.judscore = $("input[sign=c]").val();
		this.cmpscore = $("input[sign=d]").val();
		this.bosscore = $("input[sign=e]").val();
	}
	for (var i = this.inps.length - 3; i >= 0; i--) {
		this.inps.eq(i).on('blur',function(){
			that.calc();
		});
	}
}

var controller = function(){/*找到五个按钮*/
	this.run = function(){
		/*实例化元素工厂类*/
		this.el = $('#controller>tbody>tr').eq(1);
		this.el1 = this.el.children('td').eq(0);
		this.el2 = this.el.children('td').eq(1);
		this.el3 = this.el.children('td').eq(2);
		this.el4 = this.el.children('td').eq(3);
		this.el5 = this.el.children('td').eq(4);
	/* 添加添加动作 */
		this.el1.on('click',function(){
			var a = factory.createRadio();
			$('#radioarea').append(a.html());
			calccon.calc();
		});

		this.el2.on('click',function(){
		var a = factory.createCheckbox();
			$('#checkboxarea').append(a.html());
			calccon.calc();
		});

		this.el3.on('click',function(){
			var a = factory.createJudge();
			$('#judgearea').append(a.html());
			calccon.calc();
		});

		this.el4.on('click',function(){
			var a = factory.createCompletion();
			$('#completionarea').append(a.html());
			calccon.calc();
		});
		this.el5.on('click',function(){
			var a = factory.createBoss();
			$('#bossarea').append(a.html());
			calccon.calc();
		});
	}
}
var factory = new factory();
var calccon = new calccon();
var controller = new controller(); /* 寻找元素添加事件，*/
controller.run(); //执行本页面的主要js；

//每种题型最少一道，提交的时候判断一下；
var submitbtn = $('#submitbtn');
var thisform = $('#thisform');
submitbtn.click(function(e){
	e.preventDefault();   //阻止默认提交

	var res = factory.empty();
	if(res){alert('每种题型最少添加一道');return '';}
	thisform.submit(); //提交表单
});


/*  禁止表单回车实践*/
$(this).keydown( function(e) {
	var key = window.event?e.keyCode:e.which;
	if(key == "13"){
		return false;
	}
});

